package com.ht.web.emp;

import com.ht.bean.aduit.Module;
import com.ht.bean.emp.Dep;
import com.ht.bean.emp.Empinfo;
import com.ht.bean.emp.Workdiary;
import com.ht.bean.json.JsonData;
import com.ht.service.aduit.CharactersService;
import com.ht.service.aduit.CharmoduleService;
import com.ht.service.aduit.ModuleService;
import com.ht.service.emp.ChatrecordService;
import com.ht.service.emp.DepService;
import com.ht.service.emp.WorkdiaryService;
import com.ht.util.Contants;
import com.ht.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author roger
 * @date 2020/6/12 20:48
 */
@Controller
@RequestMapping("workdiary")
public class WorkdiaryController {

    @Resource
    private WorkdiaryService workdiaryService;

    @Resource
    private ChatrecordService chatrecordService;

    @Resource
    private ModuleService moduleService;

    @Resource
    private DepService depService;

    @Resource
    private CharactersService charactersService;

    @Resource
    private CharmoduleService charmoduleService;

    @Resource
    private JsonData jsonData;


    /**
     * @return 周报管理
     */
    @RequestMapping("workdiarydata")
    @ResponseBody
    public JsonData workdiarydata(@Param("limit")Integer limit , @Param("page")Integer page, String stime){
        Pager pager=new Pager();
        pager.setCurrPage(page);
        pager.setPageSize(limit);
        String date1 = null,date2 = null;
        String[] split = new String[0];
        if (stime == null || "".equals(stime)){}else{
            split = stime.split("-");
            date1 = split[0]+"-"+split[1]+"-"+split[2].split(" ")[0];
            date2 = split[3].trim()+"-"+split[4]+"-"+split[5].split(" ")[0];
        }
        jsonData.setCount(workdiaryService.allPageWorkdiaryRow(Contants.WORK_SPTG,date1,date2));
        jsonData.setData(workdiaryService.allPageWorkdiary(pager,Contants.WORK_SPTG,date1,date2));
        return jsonData;
    }

    /**
     * @return 我的周报
     */
    @RequestMapping("myworkdiarydata")
    @ResponseBody
    public JsonData myworkdiarydata(@Param("limit")Integer limit , @Param("page")Integer page, HttpServletRequest request,String stime){
        Pager pager=new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        //日期分割
        String date1 = null,date2 = null;
        String[] split = new String[0];
        if (stime == null || "".equals(stime)){}else{
            split = stime.split("-");
            date1 = split[0]+"-"+split[1]+"-"+split[2].split(" ")[0];
            date2 = split[3].trim()+"-"+split[4]+"-"+split[5].split(" ")[0];
        }
        HttpSession session = request.getSession();
        Empinfo user = (Empinfo) session.getAttribute("user");
        Integer id = user.getEmp_id();
        jsonData.setCount(workdiaryService.myworkdiaryRow(id,date1,date2));
        jsonData.setData(workdiaryService.myworkdiary(pager,id,date1,date2));
        return jsonData;
    }

    //查看周报
    @RequestMapping("searchwork")
    public String searchwork(Integer worklogid, Model model){
        List list = workdiaryService.searchWorkdiary(worklogid);
        model.addAttribute("list",list);
        return "emp/searchWorkdiary";
    }

    //去到周报修改页面
    @RequestMapping("toeditwork")
    public String toeditwork(Integer worklogid,Model model){
        Workdiary workdiary = workdiaryService.selectByPrimaryKey(worklogid);
        model.addAttribute("list",workdiary);
        return "emp/editWorkdiary";
    }

    //修改我的周報
    @RequestMapping("editwork")
    @ResponseBody
    public Integer editwork(Workdiary workdiary){
        workdiary.setCheckstate(Contants.WORK_DSP);
        workdiary.setCheckperson(" ");
        int i = workdiaryService.updateByPrimaryKeySelective(workdiary);
        return i;
    }

    //删除我的周报数据
    @RequestMapping("delworkdiary")
    @ResponseBody
    public Integer delworkdiary(Integer id,HttpSession session){
        int i = workdiaryService.deleteByPrimaryKey(id);
        int a = (int)session.getAttribute("i");
        a--;
        session.setAttribute("i",a);
        return i;
    }

    //去到添加周报页面
    @RequestMapping("toaddwork")
    public String toaddwork(HttpSession session,Map map){
        Empinfo empinfo=(Empinfo)session.getAttribute("user");
        map.put("emp",empinfo);
        return "emp/addWorkdiary";
    }

    //添加周报
    @RequestMapping("addwork")
    @ResponseBody
    public Boolean addwork(Workdiary workdiary,int emp_id,HttpSession session){
        workdiary.setEmpid(emp_id);
        Boolean bool=null;
        if((workdiaryService.selworkday(emp_id))>0){
            bool=false;
        }else {
            workdiary.setWorkday(new SimpleDateFormat("yyyy-MM-dd ").format(new Date()));
            workdiary.setCheckstate(Contants.WORK_DSP);
            workdiaryService.insert(workdiary);
            int a = (int)session.getAttribute("i");
            a++;
            session.setAttribute("i",a);
            bool=true;
        }


        return bool;
    }

    //首页周报 谈心记录条件
    @RequestMapping("homework")
    public String homework(HttpSession session,Model model){
        //周报
        Empinfo emp = (Empinfo) session.getAttribute("user");
        Integer id = emp.getEmp_id();
        int i = workdiaryService.selworkday(id);
        session.setAttribute("i",i);
        //谈心记录
        int k1=5-chatrecordService.selchatcountmonth(id);
        session.setAttribute("k",chatrecordService.selchatcountmonth(id));//已完成条数
        session.setAttribute("k1",k1);//未完成条数

        /**
         *
         * 根据角色来获取导航栏
         *
         * */
        //获取角色id,职位同等角色
        Integer jsid = emp.getPost_Id();
        List<Integer> list = charmoduleService.selectModulle(jsid);
        List<Module> modules2 = new ArrayList<>();
        List<Module> modules3 = new ArrayList<>();
        for (int mid:list){
            //获取三级
            Module module = moduleService.selectByPrimaryKey(mid);
            modules3.add(module);
            //获取二级
            if (module.getModuleid() == 37){//系统通告特殊情况
                Module parent = moduleService.selectByPrimaryKey(mid);
                modules2.add(parent);
            }else {
                Module parent = moduleService.selectByPrimaryKey(module.getRoot());
                modules2.add(parent);
            }
        }
        //去重二级
        for (int t=0;t<modules2.size()-1;t++) {
            for (int j=modules2.size()-1;j>t;j--) {
                if (modules2.get(j).getModuleid() == modules2.get(t).getModuleid()) {
                    modules2.remove(j);
                }
            }
        }
        model.addAttribute("modules2",modules2);
        model.addAttribute("modules3",modules3);
        return "managerui/index";
    }

    //周报审阅
    @RequestMapping("workcheck")
    @ResponseBody
    public JsonData workcheck( @Param("limit")Integer limit , @Param("page")Integer page,HttpSession session){
        Pager pager=new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);

        Empinfo emp = (Empinfo) session.getAttribute("user");
        Dep dep = depService.getDepByEmpid(emp.getEmp_id());
        if(dep != null){
            if(dep.getParentid() == 0){
                jsonData.setCount(workdiaryService.selcheckworkRow(Contants.WORK_DSP,dep.getDepid()));
                jsonData.setData(workdiaryService.selcheckwork(pager,Contants.WORK_DSP,dep.getDepid()));
            }else{
                jsonData.setCount(workdiaryService.selcheckworkRow2(emp.getEmp_id(),Contants.WORK_DSP,dep.getDepid()));
                jsonData.setData(workdiaryService.selcheckwork2(pager,emp.getEmp_id(),Contants.WORK_DSP,dep.getDepid()));
            }
        }else {
            jsonData.setData(null);
        }
        return jsonData;
    }

    //修改状态为审批通过
    @RequestMapping("setstate")
    @ResponseBody
    public Boolean setstate(Integer workid, HttpSession session){
        Empinfo emp =(Empinfo) session.getAttribute("user");
        Workdiary workdiary = workdiaryService.selectByPrimaryKey(workid);
        if(workdiary.getCheckstate() == Contants.WORK_DSP){
            workdiary.setCheckstate(Contants.WORK_SPTG);;
            workdiary.setChecktime(new SimpleDateFormat("yyyy-MM-dd ").format(new Date()));
            workdiary.setCheckperson(emp.getEmp_name());
        }else{
            workdiary.setCheckstate(Contants.WORK_DSP);
        }
        workdiaryService.updateByPrimaryKeySelective(workdiary);
        return true;
    }

    //修改状态为审批不通过
    @RequestMapping("setstate2")
    @ResponseBody
    public Boolean setstate2(Integer workid, HttpSession session){
        Empinfo emp =(Empinfo) session.getAttribute("user");
        Workdiary workdiary = workdiaryService.selectByPrimaryKey(workid);
        if(workdiary.getCheckstate() == Contants.WORK_DSP){
            workdiary.setCheckstate(Contants.WORK_SPBTG);
            workdiary.setChecktime(new SimpleDateFormat("yyyy-MM-dd ").format(new Date()));
            workdiary.setCheckperson(emp.getEmp_name());
            workdiary.setCheckpersonid(emp.getEmp_id());
        }else{
            workdiary.setCheckstate(Contants.WORK_DSP);
        }
        workdiaryService.updateByPrimaryKeySelective(workdiary);
        return true;
    }

    @RequestMapping("tomyworkcheck")
    public String tomyworkcheck(){
        return "emp/mycheckwork";
    }

    //我的审阅记录
    @RequestMapping("myworkcheck")
    @ResponseBody
    public JsonData myworkcheck(HttpSession session, @Param("limit")Integer limit , @Param("page")Integer page){
        Empinfo emp = (Empinfo) session.getAttribute("user");

        Pager pager=new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(workdiaryService.selMyWorkCheckRow(emp.getEmp_name()));
        jsonData.setData(workdiaryService.selMyWorkCheck(pager,emp.getEmp_name()));
        return jsonData;
    }
}
